package com.education.welco.controller.app;

import com.alibaba.fastjson.JSON;
import com.education.common.pojo.AjaxResult;
import com.education.common.pojo.TStudent;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.pojo.*;
import com.education.welco.pojo.dto.ForAppTeacherDTO;
import com.education.welco.pojo.vo.EchartsVO;
import com.education.welco.pojo.vo.StudyAnalysisParamVo;
import com.education.welco.service.IAppTeacherService;
import com.education.welco.service.ISTheoryClassroomService;
import com.education.welco.service.OrderPaperAnalysisService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 教师端app相关接口
 */
@Api(tags = {"教师端APP"})
@RestController
@RequestMapping("/app/teacher")
public class AppTeacherController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppTeacherController.class);

    @Autowired
    private IAppTeacherService appTeacherService;

    @Autowired
    private OrderPaperAnalysisService orderPaperAnalysisService;

    @Autowired
    private ISTheoryClassroomService iSTheoryClassroomService;

    /**
     * 日排课、周排课、月排课查询
     * 来源接口 com.education.welco.controller.STheoryClassroomController#selectPaike(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String)
     */
    @ApiOperation(value = "排课列表-今日-全部", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "dateType", value = "时间类型1代表一天，30天代表一个月", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页显示数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer")
    })
    @PostMapping("/sTheoryClassroom/selectPaike")
    public ApiReturnObject selectPaike(ForAppTeacherDTO dto) throws Exception {
        try {
            Integer userId = dto.getUserId();
            if (userId == null) {
                return new ApiReturnObject("201", "缺少userId", "");
            }
            Map<String, Object> map = appTeacherService.selectPaikeList(dto);
            return new ApiReturnObject("200", "成功", map);
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/selectPaike error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //新增排课获取老师
    @ApiOperation(value = "新增排课获取老师列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校id", required = false, dataType = "Integer")
    })
    @PostMapping("/sTheoryClassroom/deptAndTeaList")
    public ApiReturnObject deptAndTeaList(Integer schoolId) {
        if (schoolId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            TTeacher tTeacher = new TTeacher();
            tTeacher.setSchoolId(schoolId);
            return appTeacherService.deptAndTeaList(tTeacher);
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/deptAndTeaList error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //com.education.welco.controller.SCatalogMenuController.list
    @ApiOperation(value = "新增排课获取课程列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "学校id", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "catalogType", value = "D", required = false, dataType = "Integer")
    })
    @PostMapping("/sTheoryClassroom/sCatalogMenuList")
    public ApiReturnObject sCatalogMenuList(SCatalogMenu catalogMenu) {
        Integer userId = catalogMenu.getUserId();
        if (userId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.sCatalogMenuList(catalogMenu);
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/sCatalogMenuList error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //有个时间段的判断,判断班级是否在同一时间段有其它授课
    @ApiOperation(value = "新增排课获取班级列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attendTime", value = "开始时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    })
    @PostMapping("/sTheoryClassroom/roomList")
    public ApiReturnObject roomList(STheoryClassroom theoryClassroom) {
        Integer userId = theoryClassroom.getUserId();
        if (userId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.roomList(theoryClassroom);
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/roomList error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    /**
     * 新增排课
     * 代码来源 com.education.welco.controller.STheoryClassroomController#addPaike(com.education.welco.pojo.STheoryClassroom)
     */
    @ApiOperation(value = "新增排课", httpMethod = "Post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teaIdList", value = "老师id集合", required = true, dataType = "List"),
            @ApiImplicitParam(name = "classroomName", value = "课堂名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "attendpkTime", value = "开始时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "catalogId", value = "课程id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "classroomId", value = "班级id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "attendAddress", value = "上课地点", required = true, dataType = "String"),
            @ApiImplicitParam(name = "notice", value = "课程公告", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
    })
    @PostMapping("/sTheoryClassroom/insert")
    public ApiReturnObject addSave(STheoryClassroom sTheoryClassroom) {
        try {
//            String appJson = JSON.toJSONString(map);
//            STheoryClassroom param = JSON.parseObject(appJson, STheoryClassroom.class);
            return appTeacherService.insertSTheoryClassroom(sTheoryClassroom);
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/insert error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //课堂详情-课程目录
    @ApiOperation(value = "课程目录查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catalogId", value = "课程id", required = true, dataType = "Integer")
    })
    @PostMapping("/sTheoryClassroom/lessonCatalog")
    public ApiReturnObject lessonCatalog(Integer catalogId) {
//        Integer lessonId = Integer.valueOf(map.get("lessonId"));
        try {
            if (catalogId == null) {
                return new ApiReturnObject("201", "缺少相关参数", "");
            }
            SCatalogMenu sCatalogMenu = new SCatalogMenu();
            //查询出第1级列表
            sCatalogMenu.setParentId(0);
            sCatalogMenu.setId(catalogId);
            ApiReturnObject apiReturnObject = appTeacherService.selectSCatalogMenuList(sCatalogMenu);
            return apiReturnObject;
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/lessonCatalog error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //课程二级目录详情 来源com.education.welco.controller.SResourcesController.listOther
    //原参数 Integer catalogId, String type, String rName, Integer pageSize, Integer pageNum
//    @ApiOperation(value = "课程目录资源查询", httpMethod = "POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "catalogId", value = "课程二级目录id", required = true, dataType = "Integer"),
//            @ApiImplicitParam(name = "type", value = "资源类型（1 教学课件  2 课前导学  4 实战图片  5 实景视频  7 FLASH微课  8虚拟教学  9录播视频）", required = true, dataType = "String")
//    })
//    @PostMapping("/sTheoryClassroom/resourcesList")
//    public ApiReturnObject resourcesList(Integer catalogId, String type) {
////        Integer catalogId = Integer.valueOf(map.get("catalogId"));
////        String type = String.valueOf(map.get("type"));
////        String rName = String.valueOf(map.get("rName"));
//        if (catalogId == null) {
//            return new ApiReturnObject("201", "缺少相关参数", "");
//        }
//        try {
//            //构建对象传入参数
//            SResources resources = new SResources();
//            resources.setRCatalogid(catalogId);
////            resources.setRName(rName);
//            resources.setType(type);
//            ApiReturnObject apiReturnObject = appTeacherService.selectSResourcesList(resources);
//            return apiReturnObject;
//        } catch (Exception e) {
//            LOGGER.error("/sTheoryClassroom/resourcesList error", e);
//            return new ApiReturnObject("500", e.getMessage(), "");
//        }
//    }

    //办公管理-班级管理列表 来源com.education.welco.controller.TClassController.classListForTea
    @ApiOperation(value = "班级管理列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "教师id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页显示数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer")
    })
    @PostMapping("/tClass/classListForTea")
    public ApiReturnObject classListForTea(TClass tClass, Integer pageNum, Integer pageSize) {
        Integer userId = tClass.getUserId();
        if (userId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            List<TClass> tClasses = appTeacherService.selectClassListByTeaId(tClass);
            PageInfo<TClass> pageInfo = new PageInfo<>(tClasses);
            return new ApiReturnObject("200", "成功", pageInfo);
        } catch (Exception e) {
            LOGGER.error("/student/classListForTea error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //办公管理-学生管理-查询列表 来源com.education.welco.controller.TStudentController.stulistbytea
    @ApiOperation(value = "学生管理列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId", value = "教师id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "studentName", value = "学生姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "分页显示数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer")
    })
    @PostMapping("/tStudent/stulistbytea")
    public ApiReturnObject stulistbytea(TStudent student, Integer pageNum, Integer pageSize) {
        Integer teacherId = student.getTeacherId();
        if (teacherId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            List<TStudent> tStudents = appTeacherService.selectStudentList(student);
            PageInfo<TStudent> pageInfo = new PageInfo<>(tStudents);
            return new ApiReturnObject("200", "成功", pageInfo);
        } catch (Exception e) {
            LOGGER.error("/student/classListForTea error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }


    //办公管理-学生管理-修改 com.education.welco.controller.TStudentController.updateByTea
    @ApiOperation(value = "学生管理-修改", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "studentName", value = "学生姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "studentCode", value = "学号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "studentSex", value = "性别", required = true, dataType = "String"),
            @ApiImplicitParam(name = "idcardno", value = "身份证号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "classId", value = "班级id", required = false, dataType = "Integer"),
    })
    @PostMapping("/tStudent/updateByTea")
    private ApiReturnObject updateByTea(TStudent student) {
        Integer id = student.getId();
        Integer classId = student.getClassId();
        Integer userId = student.getUserId();
        if (id == null || classId == null || userId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            appTeacherService.updateStuByTea(student);
            return new ApiReturnObject("修改成功", "成功");
        } catch (Exception e) {
            LOGGER.error("/student/updateByTea error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //办公管理-学生管理-新增 com.education.welco.controller.TStudentController.addByTea
    @ApiOperation(value = "学生管理-添加", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentName", value = "学生姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "studentCode", value = "学号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "studentSex", value = "性别", required = true, dataType = "String"),
            @ApiImplicitParam(name = "idcardno", value = "身份证号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "classId", value = "班级id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "schoolId", value = "学校id", required = true, dataType = "Integer"),
    })
    @PostMapping("/tStudent/addByTea")
    private ApiReturnObject addByTea(TStudent student) {
        try {
            return appTeacherService.addStuByTea(student);
        } catch (Exception e) {
            LOGGER.error("/student/addByTea error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //办公管理-学生管理-通过驳回接口 com.education.welco.controller.TStudentController.addByTea
    @ApiOperation(value = "学生管理-通过驳回", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "applicationStatus", value = "状态 1通过2驳回", required = true, dataType = "Integer")
    })
    @PostMapping("/tStudent/pasrejbytea")
    private ApiReturnObject pasrejbytea(TStudent student) {
        try {
            Integer id = student.getId();
            String applicationStatus = student.getApplicationStatus();
            if (id == null || StringUtils.isBlank(applicationStatus)) {
                return new ApiReturnObject("201", "缺少相关参数", "");
            }
            return appTeacherService.pasrejbytea(student);
        } catch (Exception e) {
            LOGGER.error("/student/pasrejbytea error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "学生管理-删除", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    })
    @PostMapping("/tStudent/del")
    private ApiReturnObject del(TStudent student) {
        Integer id = student.getId();
        if (id == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.del(student);
        } catch (Exception e) {
            LOGGER.error("/student/del error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }


    //com.education.welco.controller.PlatResourceShareController.shareSchResList
    @ApiOperation(value = "学校资源-一级目录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "teaId", value = "教师id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "fileName", value = "文件名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderBy", value = "排序方式:1、名称排序 2、按大小排序 3、按创建日期排序", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "", required = true, dataType = "Integer"),
    })
    @PostMapping("/resources/shareSchResList")
    private ApiReturnObject shareSchResList(@RequestParam Map<String, String> map) {
        Integer schoolId = Integer.valueOf(map.get("schoolId"));
        Integer teaId = Integer.valueOf(map.get("teaId"));
        String fileName = map.get("fileName");
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        String orderBy = map.get("orderBy");
        if (schoolId == null || teaId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.shareSchResList(schoolId, teaId, fileName, pageNum, pageSize, orderBy);
        } catch (Exception e) {
            LOGGER.error("/resources/shareSchResList error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "学校资源-下一级目录包括文件", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "父目录id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "fileName", value = "名称", required = true, dataType = "String"),
    })
    @PostMapping("/resources/listDir")
    private ApiReturnObject listDir(@RequestParam Map<String, String> map) {
        String pid = map.get("pid");
        Long userId = Long.valueOf(map.get("userId"));
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        String fileName = String.valueOf(map.get("fileName"));
        if (pid == null || userId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.listDir(pid, userId, pageNum, pageSize, fileName);
        } catch (Exception e) {
            LOGGER.error("/resources/shareSchResList error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "学校资源-星标", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resId", value = "资源id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer"),
    })
    @PostMapping("/resources/addStar")
    private ApiReturnObject listDir(String resId, Integer userId) {
        if (resId == null || userId == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.addStar(resId, userId);
        } catch (Exception e) {
            LOGGER.error("/resources/addStar error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //com.education.welco.controller.MineResCatalogController.queryLeftMenuByTeaIdAndType(java.lang.Integer, java.lang.Integer)
    @ApiOperation(value = "我的资源库-目录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teaId", value = "老师userid", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "resType", value = "资源类型：2.视频 3.文档 1.图片", required = true, dataType = "Integer"),
    })
    @PostMapping("/resources/queryLeftMenuByTeaIdAndType")
    private ApiReturnObject queryLeftMenuByTeaIdAndType(Integer teaId, Integer resType) {
        if (teaId == null || resType == null) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.queryLeftMenuByTeaIdAndType(teaId, resType);
        } catch (Exception e) {
            LOGGER.error("/resources/queryLeftMenuByTeaIdAndType error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

//    @ApiOperation(value = "我的资源库-默认展示相关资源", httpMethod = "POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "teaId", value = "老师userid", required = true, dataType = "Integer"),
//            @ApiImplicitParam(name = "resType", value = "资源类型：2.视频 3.文档 1.图片", required = true, dataType = "Integer"),
//            @ApiImplicitParam(name = "resName", value = "资源名称", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
//            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer")
//    })
//    @PostMapping("/resources/queryAllResListByTeaIdAndType")
//    public ApiReturnObject queryAllResListByTeaIdAndType(Integer teaId, Integer resType, String resName,
//                                                         Integer pageNum, Integer pageSize) {
//        if (teaId == null || resType == null) {
//            return new ApiReturnObject("201", "缺少相关参数", "");
//        }
//        try {
//            return appTeacherService.queryAllResListByTeaIdAndType(teaId, resType, resName, pageNum, pageSize);
//        } catch (Exception e) {
//            LOGGER.error("/resources/queryAllResListByTeaIdAndType error", e);
//            return new ApiReturnObject("500", e.getMessage(), "");
//        }
//    }

    //com.education.welco.controller.MineResCatalogController.queryLeftMenuByTeaIdAndType(java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.Integer)
    @ApiOperation(value = "我的资源库-根据目录查询相关资源", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catalogId", value = "目录id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "resName", value = "资源名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderBy", value = "排序方式:1、名称排序 2、按大小排序 3、按创建日期排序", required = false, dataType = "String")
    })
    @PostMapping("/resources/queryResListByCatalogId")
    public ApiReturnObject queryResListByCatalogId(Integer catalogId, String resName, Integer resType,
                                                   Integer pageNum, Integer pageSize, String orderBy) {
//        if (catalogId == null) {
//            return new ApiReturnObject("201", "缺少相关参数", "");
//        }
        try {
            return appTeacherService.queryResListByCatalogId(catalogId, resName, pageNum, pageSize, orderBy, resType);
        } catch (Exception e) {
            LOGGER.error("/resources/queryResListByCatalogId error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "我的资源库-修改资源名称", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mineResId", value = "资源id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "mineResName", value = "资源名称", required = true, dataType = "String")
    })
    @PostMapping("/resources/update")
    public ApiReturnObject editSave(Integer mineResId, String mineResName) {
        if (mineResId == null || StringUtils.isBlank(mineResName)) {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
        try {
            return appTeacherService.updateResourceName(mineResId, mineResName);
        } catch (Exception e) {
            LOGGER.error("/resources/queryResListByCatalogId error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "回收站-列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teaId", value = "教师id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "resType", value = "资源类型", required = true, dataType = "String"),
            @ApiImplicitParam(name = "resName", value = "资源名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "orderBy", value = "1.名称 2.大小 3.人员 4.删除日期", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer")
    })
    @PostMapping("/mineResTable/recycleList")
    public ApiReturnObject recycleList(Integer teaId, Integer type,
                                       String resName, String orderBy,
                                       Integer pageNum, Integer pageSize) {
        try {
            return appTeacherService.recycleList(teaId, type, resName, orderBy, pageNum, pageSize);
        } catch (Exception e) {
            LOGGER.error("/mineResTable/recycleList error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    /**
     * 教师APP-回收站一键清空
     *
     * @param teaUserId 教师用户id
     * @return
     */
    @ApiOperation(value = "回收站一键清空", httpMethod = "POST")
    @PostMapping("/mineResTable/oneClickClear")
    public ApiReturnObject oneClickClear(Integer teaUserId) {
        try {
            return appTeacherService.oneClickClear(teaUserId);
        } catch (Exception e) {
            LOGGER.error("/mineResTable/oneClickClear error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }

    //用户协议
    @ApiOperation(value = "用户协议", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "类型 1.用户协议", required = true, dataType = "String"),
    })
    @PostMapping("/currency/agreement")
    public ApiReturnObject agreement(Integer userId, String type) {
        try {
            if (StringUtils.isBlank(type)) {
                return new ApiReturnObject("201", "缺少相关参数", "");
            }
            return appTeacherService.agreement(userId, type);
        } catch (Exception e) {
            LOGGER.error("/mineResTable/agreement error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
    }


    //测试
    @PostMapping("/test")
    public ApiReturnObject testApp(@RequestParam Map<String, String> map) {
        try {
            String appJson = JSON.toJSONString(map);
            Brand brand = JSON.parseObject(appJson, Brand.class);
            System.out.println(111);
            appTeacherService.testTransActional();
        } catch (Exception e) {
            LOGGER.error("/sTheoryClassroom/insert error", e);
            return new ApiReturnObject("500", e.getMessage(), "");
        }
        return new ApiReturnObject("200", "成功");
    }


    //平均分，总分，最高分，最低分，平均作答时长，及格率，各个分段人数
    //1 80-89分以上
    //2 60-80分段
    //3 90-99分段
    //4 60-69分段
    //5 100分
    @GetMapping("/summary")
    public AjaxResult summary(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.summary(paperId);
            List<EchartsVO> echartsVOS = (List<EchartsVO>) map.get("numberOfPeopleDivided");
            List<Object> objects = new ArrayList<>();
            for (EchartsVO echartsVO : echartsVOS) {
                List<Object> objects1 = new ArrayList<>();
                objects1.add(echartsVO.getName());
                objects1.add(echartsVO.getValue());
                objects.add(objects1);
            }
            map.put("iosData", objects);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("summary error", e);
            return AjaxResult.error();
        }
    }


    //试题类型分布
    @GetMapping("/paperQuestionTypeDistribution")
    public AjaxResult paperQuestionType(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.paperQuestionType(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("paperQuestionType error", e);
            return AjaxResult.error();
        }
    }

    //试题分数分布
    @GetMapping("/paperScoreDistribution")
    public AjaxResult paperScoreDistribution(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.paperScoreDistribution(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("paperScoreDistribution error", e);
            return AjaxResult.error();
        }
    }

    //易错题top10
    @GetMapping("/fallibleQuestionTop")
    public AjaxResult fallibleQuestionTop(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.fallibleQuestionTop(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("fallibleQuestionTop error", e);
            return AjaxResult.error();
        }
    }


    //每道题选择人数
    @GetMapping("/itemAnalysis")
    public AjaxResult itemAnalysis(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.itemAnalysis(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("itemAnalysis error", e);
            return AjaxResult.error();
        }
    }

    //学情分析
    //视频总时长，线上考核参与人数，平均学习进度，测试及格率
    //每道题选择人数
    @GetMapping("/dataScreening")
    public AjaxResult dataScreening(@RequestParam(value = "courseId", required = true) Integer courseId) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.dataScreening(courseId, classId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("dataScreening error", e);
            return AjaxResult.error();
        }
    }

    /**
     * 学习进度-明细
     *
     * @param courseId
     * @return
     */
    @GetMapping("/rateOfLearning")
    public AjaxResult rateOfLearning(@RequestParam(value = "courseId", required = true) Integer courseId,
                                     Integer pageNum, Integer pageSize) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.rateOfLearningApp(courseId, classId, pageNum, pageSize);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("rateOfLearning error", e);
            return AjaxResult.error();
        }
    }

    /**
     * 学习进度-环形图
     *
     * @param courseId
     * @return
     */
    @GetMapping("/rateOfLearningEcharts")
    public AjaxResult rateOfLearningEcharts(@RequestParam(value = "courseId", required = true) Integer courseId) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.rateOfLearningEcharts(courseId, classId);
            List<EchartsVO> echartsVOS = (List<EchartsVO>) map.get("echartsData");
            List<Object> objects = new ArrayList<>();
            for (EchartsVO echartsVO : echartsVOS) {
                List<Object> objects1 = new ArrayList<>();
                objects1.add(echartsVO.getName());
                objects1.add(echartsVO.getValue());
                objects.add(objects1);
            }
            map.put("iosData", objects);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("rateOfLearningEcharts error", e);
            return AjaxResult.error();
        }
    }

    //视频学习情况-柱形图
    @GetMapping("/videoLearningEcharts")
    public AjaxResult videoLearningEcharts(Integer courseId, Integer catalogId) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.videoLearningEcharts(buildParam(courseId, classId, catalogId));
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("videoLearningEcharts error", e);
            return AjaxResult.error();
        }
    }

    //视频学习情况-已学习和未学习
    @GetMapping("/videoLearning")
    public AjaxResult videoLearning(Integer courseId, Integer catalogId, Integer ifLearn,
                                    Integer pageNum, Integer pageSize) {
        try {
            Integer classId = getClassId(courseId);
            StudyAnalysisParamVo param = buildParam(courseId, classId, catalogId);
            param.setIfLearn(ifLearn);
            Map<String, Object> map = orderPaperAnalysisService.videoLearningApp(param, pageNum, pageSize);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("videoLearning error", e);
            return AjaxResult.error();
        }
    }


    //考核完成情况-柱状图
    @GetMapping("/assessmentCompletionEcharts")
    public AjaxResult assessmentCompletion(Integer courseId, Integer catalogId) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.assessmentCompletionEcharts(buildParam(courseId, classId, catalogId));
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("assessmentCompletion error", e);
            return AjaxResult.error();
        }
    }

    //考核完成情况-已学习和未学习
    @GetMapping("/assessmentCompletion")
    public AjaxResult assessmentCompletion(Integer courseId, Integer catalogId, Integer ifLearn,
                                           Integer pageNum, Integer pageSize) {
        try {
            Integer classId = getClassId(courseId);
            StudyAnalysisParamVo param = buildParam(courseId, classId, catalogId);
            param.setIfLearn(ifLearn);
            Map<String, Object> map = orderPaperAnalysisService.assessmentCompletionApp(param, pageNum, pageSize);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("assessmentCompletion error", e);
            return AjaxResult.error();
        }
    }

    //内容榜单
    @GetMapping("/contentList")
    public AjaxResult contentList(Integer courseId) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.contentList(buildParam(courseId, classId, null));
            Map<String, Object> map1 = orderPaperAnalysisService.talkTop(buildParam(courseId, classId, null));
            map.put("talkTopList", map1.get("dataList"));
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("contentList error", e);
            return AjaxResult.error();
        }
    }

    //参与率最高的讨论主题TOP3
    @GetMapping("/talkTop")
    public AjaxResult talkTop(Integer courseId) {
        try {
            Integer classId = getClassId(courseId);
            Map<String, Object> map = orderPaperAnalysisService.talkTop(buildParam(courseId, classId, null));
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("talkTop error", e);
            return AjaxResult.error();
        }
    }

    /**
     * @param courseId
     * @param classId
     * @param catalogId
     * @return
     */
    private StudyAnalysisParamVo buildParam(Integer courseId, Integer classId, Integer catalogId) {
        StudyAnalysisParamVo paramVo = new StudyAnalysisParamVo();
        paramVo.setCourseId(courseId);
        paramVo.setClassId(classId);
        paramVo.setCatalogId(catalogId);
        return paramVo;
    }

    /**
     * 获取班级id
     *
     * @param courseId
     * @return
     */
    private Integer getClassId(Integer courseId) {
        STheoryClassroom sTheoryClassroom = iSTheoryClassroomService.selectSTheoryClassroomById(courseId);
        Integer classId = sTheoryClassroom.getClassroomId();
        return classId;
    }
}
